package com.jtech.together.view.fragment;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jtech.together.R;
import com.jtech.together.model.FriendModel;
import com.jtech.together.model.event.AcceptEvent;
import com.jtech.together.mvp.contract.FriendsContract;
import com.jtech.together.mvp.presenter.FriendsPresenter;
import com.jtech.together.util.ActivityJump;
import com.jtech.together.util.Bus;
import com.jtech.together.view.activity.UserProfileActivity;
import com.jtech.together.view.adapter.FriendsAdapter;
import com.jtech.together.view.fragment.base.BaseFragment;
import com.jtechlib2.listener.OnItemClickListener;
import com.jtechlib2.view.JRecyclerView;
import com.jtechlib2.view.RecyclerHolder;
import com.jtechlib2.view.RefreshLayout;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import cc.solart.wave.WaveSideBarView;

/**
 * 我的好友
 * Created by wuxubaiyang on 2017/5/10.
 */
public class FriendsFragment extends BaseFragment implements FriendsContract.View, RefreshLayout.OnRefreshListener, OnItemClickListener,
        WaveSideBarView.OnTouchLetterChangeListener {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.refreshlayout_friends)
    RefreshLayout refreshLayout;
    @Bind(R.id.jrecyclerview_friends)
    JRecyclerView jRecyclerView;
    @Bind(R.id.wavesidebar_friends)
    WaveSideBarView waveBar;
    @Bind(R.id.textview_friend_none)
    TextView textViewNone;

    private LinearLayoutManager linearLayoutManager;
    private FriendsContract.Presenter presenter;
    private FriendsAdapter friendsAdapter;

    @Override
    public View createView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
        return layoutInflater.inflate(R.layout.fragment_friends, viewGroup, false);
    }

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new FriendsPresenter(getActivity(), this, bundle);
        //上车
        Bus.getOn(this);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        //设置layoutmanager
        linearLayoutManager = new LinearLayoutManager(getActivity());
        jRecyclerView.setLayoutManager(linearLayoutManager);
        //设置适配器
        friendsAdapter = new FriendsAdapter(getActivity());
        jRecyclerView.setAdapter(friendsAdapter);
        //设置下拉刷新
        refreshLayout.setOnRefreshListener(this);
        //设置tiem点击事件
        jRecyclerView.setOnItemClickListener(this);
        //设置索引条事件
        waveBar.setOnTouchLetterChangeListener(this);
    }

    @Override
    protected void loadData() {
        //刷新好友
        presenter.loadFriendList();
    }

    public static FriendsFragment newInstance() {
        Bundle args = new Bundle();
        FriendsFragment fragment = new FriendsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 接受添加好友请求
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void acceptAddFriend(AcceptEvent event) {
        presenter.loadFriendList();
    }

    @Override
    public void loadSuccess(List<FriendModel> friendModels) {
        refreshLayout.refreshingComplete();
        friendsAdapter.setDatas(friendModels);
        //设置标题栏人数
        toolbar.setTitle("我的好友(" + friendModels.size() + ")");
        //是否显示空图标
        textViewNone.setVisibility(friendModels.size() <= 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void loadFail(String error) {
        refreshLayout.refreshingComplete();
        Snackbar.make(toolbar, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh() {
        presenter.loadFriendList();
    }

    @Override
    public void onLetterChange(String letter) {
        int position = friendsAdapter.getKeyWordFirstShowPosition(letter);
        //滚动列表到指定位置
        jRecyclerView.scrollToPosition(position);
        //linearlayoutmanager滚动
        linearLayoutManager.scrollToPositionWithOffset(position, 0);
    }

    @Override
    public void onItemClick(RecyclerHolder holder, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(UserProfileActivity.KEY_USER_OBJECT_ID,
                friendsAdapter.getFriendObjectId(holder.getAdapterPosition()));
        ActivityJump.build(getActivity(), UserProfileActivity.class)
                .addBundle(bundle)
                .jump();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //下车
        Bus.getOff(this);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
    }
}